package com.chouren.goandroid.ui.progressbar;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Paint.Align;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.view.View;

import com.chouren.goandroid.R;

public class RoundProgressBar extends View {
	private Paint paint;
	private Paint textPaint;
	private Paint progressPaint;
	private int roundColor;
	private int progressBarColor;
	private int textColor;
	private float textSize;
	private float radius;
	private int maxValue;
	private int progressValue;
	private boolean showText;
	private Style style;
	private RectF oval;
	
	public enum Style {STROKE, FILL}

	public RoundProgressBar(Context context) {
		this(context, null);
	}

	public RoundProgressBar(Context context, AttributeSet attrs) {
		this(context, attrs, 0);
	}
	
	public RoundProgressBar(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		
		TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.RoundProgressBar);
		roundColor = typedArray.getColor(R.styleable.RoundProgressBar_roundColor, Color.GRAY);
		progressBarColor = typedArray.getColor(R.styleable.RoundProgressBar_progressBarColor, Color.YELLOW);
		textColor = typedArray.getColor(R.styleable.RoundProgressBar_textColor, Color.WHITE);
		textSize = typedArray.getDimension(R.styleable.RoundProgressBar_textSize, 46);
		maxValue = typedArray.getInteger(R.styleable.RoundProgressBar_maxValue, 100);
		showText = typedArray.getBoolean(R.styleable.RoundProgressBar_showText, true);
		radius = typedArray.getDimension(R.styleable.RoundProgressBar_rad, 80);
		if(typedArray.getInt(R.styleable.RoundProgressBar_style, 0) == 0) style = Style.STROKE;
		else style = Style.FILL;
		typedArray.recycle();
		
		init();
	}
	
	private void init() {
		paint = new Paint();
		
		paint.setColor(roundColor);
		paint.setStyle(Paint.Style.STROKE);
		paint.setAntiAlias(true);
		paint.setStrokeWidth(radius * 0.1f);
		
		textPaint = new Paint();
		textPaint.setAntiAlias(true);
		textPaint.setColor(textColor);
		textPaint.setTextSize(textSize);
		textPaint.setStyle(Paint.Style.STROKE);
		textPaint.setTextAlign(Align.CENTER);
		
		progressPaint = new Paint();
		progressPaint.setColor(progressBarColor);
		progressPaint.setStyle(Paint.Style.STROKE);
		progressPaint.setAntiAlias(true);
		progressPaint.setStrokeWidth(radius * 0.12f);
		
		oval = new RectF();
	}
	
	@Override
	protected void onDraw(Canvas canvas) {
		super.onDraw(canvas);
		canvas.drawCircle(getWidth()/2, getHeight()/2, radius, paint);
		
		String text = (progressValue % maxValue) + "%";
		canvas.drawText(text, getWidth()/2, getHeight()/2 + textSize / 4, textPaint);
		
		oval.set(getWidth()/2 - radius, getHeight()/2 - radius, getWidth()/2 + radius, getHeight()/2 + radius);
		switch (style) {
		case STROKE:
			paint.setStyle(Paint.Style.STROKE);
			break;
		case FILL:
			paint.setStyle(Paint.Style.FILL);
			break;
		}
		canvas.drawArc(oval, 0, 360 * progressValue/maxValue, false, progressPaint);
	}
	

	public Paint getPaint() {
		return paint;
	}

	public void setPaint(Paint paint) {
		this.paint = paint;
	}

	public int getRoundColor() {
		return roundColor;
	}

	public void setRoundColor(int roundColor) {
		this.roundColor = roundColor;
	}

	public int getProgressBarColor() {
		return progressBarColor;
	}

	public void setProgressBarColor(int progressBarColor) {
		this.progressBarColor = progressBarColor;
	}

	public int getTextColor() {
		return textColor;
	}

	public void setTextColor(int textColor) {
		this.textColor = textColor;
	}

	public float getTextSize() {
		return textSize;
	}

	public void setTextSize(float textSize) {
		this.textSize = textSize;
	}

	public float getRadius() {
		return radius;
	}

	public void setRadius(float radius) {
		this.radius = radius;
	}

	public int getMaxValue() {
		return maxValue;
	}

	public void setMaxValue(int maxValue) {
		this.maxValue = maxValue;
	}

	public int getProgressValue() {
		return progressValue;
	}

	public void setProgressValue(int progressValue) {
		this.progressValue = progressValue;
		postInvalidate();
	}

	public boolean isShowText() {
		return showText;
	}

	public void setShowText(boolean showText) {
		this.showText = showText;
	}

	public Style getStyle() {
		return style;
	}

	public void setStyle(Style style) {
		this.style = style;
	}
}
